<template>
  <van-row class="page-rectify-list">
    <van-nav-bar left-text="返回" :right-text="(rectifyList.length > 0 && step === $vo.RectStep.FIND) ? '指定责任部门' : ''" :title="dictRectifyStep(step)" @click-left="$router.back()" @click-right="endSelectDept"/>
    <panel-rectify-filter :assessType="assessType" :step="step" :default="dictStepToSelect(this.step)" @onSelect="doSelectCondition"/>
    <template v-if="step === $vo.RectStep.APPR || step === $vo.RectStep.ACCE">
      <van-row style="padding: 10px 16px; background-color: white">
        <van-button style="" type="info" block @click="approveAll">{{approveAllText}}</van-button>
      </van-row>
    </template>
    <van-tabs v-model="tabActive">
      <van-tab title="公司发现问题" name="0"/>
      <van-tab title="单位自查问题" name="1"/>
    </van-tabs>
    <van-empty v-if="showEmpty" description="无问题整改信息" />
    <panel-rectify-list ref="mychild" :items="rectifyList" @click="doShowDetail"/>
    <van-loading v-if="listLoad" class="query-load" type="spinner" >努力加载中...</van-loading>
    <van-row v-if="!listFinished && rectifyList.length >= condition.pageSize" @click="doGetRectifyList" class="query-load">
      {{listError ? '载入失败，重新加载...' : '加载更多...'}}
    </van-row>
    <van-popup
      v-model="showPageRectifyDetail"
      position="right"
      get-container="body"
      style="width: 100%; height: 100%; background-color: #F9F9F9">
      <page-rectify-detail v-if="showPageRectifyDetail" :rectify="selectedRectify" :assessType="assessType" :step="step" @onCancel="showPageRectifyDetail = false" @onConfirm="doConfirm"/>
    </van-popup>
    <van-popup
      v-model="showPageDeptList"
      position="right"
      get-container="body"
      style="width: 100%; height: 100%; background-color: #F9F9F9">
      <page-dept-list v-if="showPageDeptList" @onCancel="showPageDeptList = false" @onConfirm="endSelectDept"/>
    </van-popup>
  </van-row>
</template>

<script>
import DictMixin from '@/mixins/DictMixin'
import PanelRectifyFilter from '@/views/rectify/common/panel/PanelRectifyFilter'
import PanelRectifyList from '@/views/rectify/common/panel/PanelRectifyList'
import PageRectifyDetail from '@/views/rectify/PageRectifyDetail'
import PageDeptList from '@/views/rectify/common/page/PageDeptList'
import { getCompanyRectifyList, getDeptRectifyList, batchRectifyApprove, batchRectifyDuty } from '@/api/rectify'

export default {
  name: 'PageRectifyList',
  props: ['assessType', 'step'],
  mixins: [DictMixin],
  components: { PanelRectifyFilter, PanelRectifyList, PageRectifyDetail, PageDeptList },
  data () {
    return {
      showPageRectifyDetail: false,
      showPageDeptList: false,
      listError: false,
      listLoad: false,
      listFinished: false,
      tabActive: '0',
      showEmpty: false,
      condition: this.$vo.createCondition(),
      rectifyList: [],
      selectedRectify: '',
      con:true
    }
  },
  created(){

  },
  mounted () {

    console.log(this.$route.query)
      //this.condition.rectify_status = (this.$route.query.dbstatus == 5 || this.$route.query.dbstatus == 8) ? 'rectify' :  this.dictStepToSelect(this.step)
      this.condition.rectify_status = (this.$route.query.dbstatus == 2 || this.$route.query.dbstatus == 5) ? 'rectify' :  (this.$route.query.dbstatus == 4 || this.$route.query.dbstatus == 8) ? 'commit' : this.dictStepToSelect(this.step)
      console.log(this.step)
      console.log(this.condition.rectify_status)
      this.condition.user_dept = this.$route.query.bmmc != undefined ? this.$route.query.bmmc : this.$store.state.authInfo.rectifyDept.department_path

      console.log(this.$store.state.authInfo)
      this.condition.op = (this.$route.query.dbstatus == 5 || this.$route.query.dbstatus == 8) ? 'manage' : this.dictRectifyOperate(this.step)
      this.condition.problem_type = this.tabActive
      //待办跳转是公司发现问题 还是 单位自查问题
    if(this.$route.query.sscm == 1){
      this.tabActive = '1'
      this.con=false
    }
    if(this.con){
      this.doGetRectifyList()
    }



    
  },
  methods: {
    endSelectDept () {
      // this.showPageDeptList = false
      let params = {
        user_dept: this.$route.query.bmmc != undefined ? this.$route.query.bmmc : this.$store.state.authInfo.rectifyDept.department_path,
        user_id: this.$store.state.authInfo.user.userid,
        user_name: this.$store.state.authInfo.user.username,
        problem_type: this.tabActive,
        duty_dept: '',
        approve_type: this.$vo.RectStatus.TASK,
        rectify_type: this.assessType === this.$vo.AssessType.CORP + '' ? 'company' : 'dept'
      }
      batchRectifyDuty(params).then(res => {
        let { data } = res
        if (data[this.$response.CODE] === this.$response.SUCCESS) {
          this.$toast.success('批量指定部门成功')
        } else {
          this.$toast.fail('批量指定部门失败：' + data[this.$response.MESSAGE])
        }
      }, errFunc => {
        this.$notify('服务调用失败：' + errFunc.toString())
        console.log(errFunc.toString())
      }).finally(() => {
        this.rectifyList = []
        this.doGetRectifyList()
      })
    },
    approveAll () {
      let obj = {}
      obj.user_dept = this.condition.user_dept
      obj.user_id = this.$store.state.authInfo.user.userid
      obj.user_name = this.$store.state.authInfo.user.username
      obj.problem_type = this.tabActive
      obj.approve_type = this.apprType
      obj.approve_result = '0'
      obj.rectify_type = this.assessType === '0' ? 'company' : 'dept'
      batchRectifyApprove(obj).then(res => {
        let { data } = res
        if (data[this.$response.CODE] === this.$response.SUCCESS) {
          this.$toast.success('批量审核成功')
        } else {
          this.$toast.fail('批量审核失败：' + data[this.$response.MESSAGE])
        }
      }, errFunc => {
        this.$notify('服务调用失败：' + errFunc.toString())
        console.log(errFunc.toString())
      }).finally(() => {
        this.rectifyList = []
        this.doGetRectifyList()
      })
    },
    doShowDetail (value) {
      console.log(value)
      this.selectedRectify = value
      this.showPageRectifyDetail = true
    },
    doConfirm () {
      this.rectifyList = []
      this.condition.pageNum = 1
      this.doGetRectifyList()
      this.showPageRectifyDetail = false
    },
    doSelectCondition (value) {
      console.log(value)
      // if(this.$route.query.dbstatus == 5 || this.$route.query.dbstatus == 8){
      //   this.status = 'rectify'
      //   this.type = 'manage'
      // }
      if (value.type === 'manage') {
        this.condition.op = value.type
      } else {
        this.condition.op = this.dictRectifyOperate(this.step)
      }
      this.condition.carrier = value.carrier
      this.condition.rectify_status = (this.$route.query.dbstatus == 5 || this.$route.query.dbstatus == 8 ) ? 'rectify' : value.status 
      this.condition.pageNum = 1
      this.rectifyList = []
      this.doGetRectifyList()
    },
    doGetRectifyList () {
      this.listLoad = true
      this.listError = false
      let request = this.assessType === this.$vo.AssessType.CORP + '' ? getCompanyRectifyList : getDeptRectifyList
      console.log('condition', this.condition)
      request(this.condition).then(res => {
        this.listLoad = false
        let { data } = res
        if (data[this.$response.CODE] === this.$response.SUCCESS) {
          let listData = data[this.$response.DATA].list ? data[this.$response.DATA].list : []
          if (listData.length > 0) {
            // 调用子组件里面得方法
            this.$nextTick(() =>{
              this.$refs.mychild.clickscrollPage();
            })

            // 将返回数据追加到展示列表中
            this.rectifyList = this.rectifyList.concat(listData)

            // 判断后续是否还有数据
            this.listFinished = this.rectifyList.length === data[this.$response.DATA].total

            // 下次查询页增加1
            this.condition.pageNum = this.condition.pageNum + 1
          }
        } else {
          this.$toast.fail('获取数据失败：' + data[this.$response.MESSAGE])
          this.listError = true
        }
        this.showEmpty = this.rectifyList.length === 0
      }, errFunc => {
        this.listLoad = false
        this.$notify('服务调用失败：' + errFunc.toString())
        console.log(errFunc.toString())
      })
    }
  },
  watch: {
    tabActive: function () {
      this.condition.problem_type = this.tabActive
      this.condition.pageNum = 1
      this.rectifyList = []
      this.doGetRectifyList()
    }
  },
  computed: {
    apprType: function () {
      if (this.condition.op === 'review') {
        if (this.step === this.$vo.RectStep.APPR) {
          return 'review_approve'
        } else {
          return 'review_complete'
        }
      } else {
        if (this.step === this.$vo.RectStep.APPR) {
          return 'manage_approve'
        } else {
          return 'manage_complete'
        }
      }
    },
    approveAllText: function () {
      if (this.step === this.$vo.RectStep.APPR) {
        return '全部审核通过'
      }
      if (this.step === this.$vo.RectStep.ACCE) {
        return '全部验收通过'
      }

      return ''
    }
  }
}
</script>

<style scoped lang="less">
.page-rectify-list{
  /deep/ .card-grade-sketch{
    margin-top: 3px;
  }
  .query-load{
    padding: 10px 0px;
    text-align: center;
  }
  .bottom-action-bar{
    display: flex;
    box-sizing: content-box;
    width: 100%;
    height: 50px;
    position: fixed;
    bottom: 0;
    left: 0;
    background-color: #ffffff;
  }
  .bottom-action-item{
    color: #1989fa;
    display: flex;
    -webkit-box-flex: 1;
    flex: 1;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    flex-direction: column;
    -webkit-box-align: center;
    align-items: center;
    -webkit-box-pack: center;
    justify-content: center;
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    .btn-icon{
      position: relative;
      margin-bottom: 5px;
      font-size: 18px;
    }
  }
  .bottom-action-bar::after{
    position: absolute;
    box-sizing: border-box;
    content: ' ';
    pointer-events: none;
    right: 0;
    bottom: 0;
    left: 16px;
    border-top: 1px solid #ebedf0;
    -webkit-transform: scaleY(.5);
    transform: scaleY(.5);
  }
}
</style>
